package com.wmlive.hhvideo.heihei.websocket;

import android.content.Context;
import android.content.Intent;

import com.wmlive.hhvideo.application.DCApplication;
import com.wmlive.hhvideo.utils.ServiceUtils;

/**
 * websocketService 管理
 * Created by kangzhen on 2017/7/6.
 */

public class WebSocketServiceManager {
    private Context mContext;

    private static WebSocketServiceManager manager;
    public static WebSocketServiceManager getInstance(Context context){
        if(manager==null){
            synchronized(WebSocketServiceManager.class){
                if(manager==null){
                    manager = new WebSocketServiceManager(context);
                }
            }
        }
        return manager;
    }

    private WebSocketServiceManager(Context context){
        mContext = context;
    }
    private boolean isWebSocketServiceRunning(){
      return   ServiceUtils.isServiceRunning(DCApplication.getDCApp(),"com.wmlive.hhvideo.heihei.websocket.MyWebsocketService");
    }

    /**
     * 开启服务
     */
    public  void startWebSocketService(){
        Intent websockeServiceIntent = new Intent();
        if(isWebSocketServiceRunning()){
              //重新启动
            websockeServiceIntent.setAction(MyWebsocketService.MYWEBSOCKET_CHECK_SERVER_RECONNECTION);
        }else {
            websockeServiceIntent.setAction(MyWebsocketService.MYWEBSOCKET_CHECK_SERVER_START);
        }
        websockeServiceIntent.setPackage(mContext.getPackageName());
        DCApplication.getDCApp().startService(websockeServiceIntent);
    }

    /**
     * 重新启动服务
     */
    public void reStartWebSocketService(){
        Intent websockeServiceIntent = new Intent(DCApplication.getDCApp(),MyWebsocketService.class);
        if(!isWebSocketServiceRunning()){
            websockeServiceIntent.setAction(MyWebsocketService.MYWEBSOCKET_CHECK_SERVER_START);
        }else {
            websockeServiceIntent.setAction(MyWebsocketService.MYWEBSOCKET_CHECK_SERVER_RECONNECTION);
        }
        DCApplication.getDCApp().startService(websockeServiceIntent);
    }

    /**
     * 停止服务
     */
    public void stopWebSocketService(){
        Intent websockeServiceIntent = new Intent(DCApplication.getDCApp(),MyWebsocketService.class);
        websockeServiceIntent.setAction(MyWebsocketService.MYWEBSOCKET_CHECK_SERVER_STOP);
        DCApplication.getDCApp().startService(websockeServiceIntent);
    }

}
